package com.example;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * User: walk_lai
 * Date: 12-12-10
 * Time: 下午10:16
 */
public class PasswordConfigActivity extends Activity {
    SharedPreferences sharedPref;
    String password = null;

    private EditText oldPassword;
    private EditText confirmPassword;
    private EditText newPassword;
    private Button confirmButton;
    private Button cancelButton;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_password);
        Context context = PasswordConfigActivity.this;

        oldPassword = (EditText) findViewById(R.id.oldPassword);
        confirmPassword = (EditText) findViewById(R.id.confirmPassword);
        newPassword = (EditText) findViewById(R.id.newPassword);
        confirmButton = (Button) findViewById(R.id.confirmButton);
        cancelButton = (Button) findViewById(R.id.cancelButton);

        sharedPref = context.getSharedPreferences(
                "password_cfg", Context.MODE_PRIVATE);

        password = sharedPref.getString("password", null);
        if (password == null) password = "123456";

        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!password.equals(oldPassword.getText().toString())) {
                    Toast.makeText(PasswordConfigActivity.this, "Old password is wrong.", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!newPassword.getText().toString().equals(confirmPassword.getText().toString())) {
                    Toast.makeText(PasswordConfigActivity.this, "two password must be same .", Toast.LENGTH_SHORT).show();
                    confirmPassword.setText("");
                    confirmPassword.findFocus();
                    return;
                }
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("password", newPassword.getText().toString());
                editor.commit();

                Toast.makeText(PasswordConfigActivity.this, "password updated.", Toast.LENGTH_SHORT).show();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PasswordConfigActivity.this.finish();
            }
        });

    }
}